package com.ge.boot06core.service;

import com.ge.boot06core.entity.UserEntity;
import com.ge.boot06core.event.LoginSuccessEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

/**
 * 测试基于事件驱动开发<br>
 * 账户服务<br>
 * 通过实现ApplicationListener接口，指定事件类型，对登录成功事件进行监听
 */

@Service
public class AccountService implements ApplicationListener<LoginSuccessEvent> {

    public void addAccountScore(String userName) {

        System.out.println(userName + " 账户增加了积分");

    }

    @Override
    public void onApplicationEvent(LoginSuccessEvent event) {

        System.out.println("AccountService 接收到事件");
        // 从事件数据中获取用户登录信息
        UserEntity source = (UserEntity) event.getSource();
        addAccountScore(source.getUserName());

    }
}
